//
//  RecordOperationBubbleViewController.m
//  SharePopViewDemo
//
//  Created by YangShuai on 16/6/27.
//  Copyright © 2016年 ys. All rights reserved.
//

#import "RecordOperationBubbleViewController.h"
#import "TLBubbleView.h"
#import "JRMasonry.h"
#import "UIColor+Hex.h"
#import "ConstDefine.h"

@interface RecordOperationBubbleViewController ()
{
    TLBubbleView* bubbleView;
}

@property (nonatomic,copy)ClickButtonBlock clickButton;
@property (nonatomic,copy)clickEmptyBlock clickEmpty;

@end

@implementation RecordOperationBubbleViewController

-(void)viewDidLoad{
    self.title = nil;
    if ([DataProcessing iOS7_low]) {
        CGRect frame = [DataProcessing exchangeWH:self.view.bounds];
        self.view.frame = frame;
    }
    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEmpty)];
    [self.view addGestureRecognizer:tap];
    //
    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapEmpty)];
    [self.view addGestureRecognizer:panGR];
}

-(void)tapEmpty{
    BOOL hideBubble = _clickEmpty();
    if (hideBubble) {
        [self _tapEmpty];
    }
}

- (void)_tapEmpty
{
    //
    CGPoint pt = bubbleView.center;
    pt.x += bubbleView.frame.size.width*0.5;
    [UIView animateWithDuration:0.3f animations:^{
        bubbleView.center = pt;
        bubbleView.transform = CGAffineTransformMakeScale(0.000001, 1);
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self willMoveToParentViewController:nil];
        [self removeFromParentViewController];
    }];
}


- (void)showRecordOperationBubbleViewWithParentVC:(UIViewController*)parentVC ClickButton:(ClickButtonBlock)clickButton ClickEmpty:(clickEmptyBlock)clickEmpty{
    if (!parentVC) {
        return;
    }
    self.clickButton = clickButton;
    self.clickEmpty = clickEmpty;
    
    CGFloat width = 270;
    CGFloat height = 80;
    CGFloat distance = 30;
    CGFloat radius = 12;
    CGFloat arrowHeight = 12;
    
    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        width = 132;
        height = 38;
        distance = 16;
        radius = 6;
        arrowHeight = 6;
    }
    
    //添加到父VC上
    [parentVC addChildViewController:self];
    [parentVC.view addSubview:self.view];
    [self didMoveToParentViewController:parentVC];
    CGPoint pt = self.view.center;
    if (_followView) {
        pt = [_followView.superview convertPoint:_followView.center toView:self.view];
        pt.x -= _followView.frame.size.height*0.86;
    }
    if (!bubbleView) {
        bubbleView = [TLBubbleView TLBubbleViewWithSize:CGSizeMake(width,height)];
        [self.view addSubview:bubbleView];
        //
        for (int i = 0; i < 3; i++) {
            UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(23 + distance * i + 50 * i, 15, 50, 50);
            if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
                button.frame = CGRectMake(10 + distance * i + 24 * i, 7, 24, 24);
            }
            [bubbleView addSubview:button];
            switch (i) {
                case 0:
                    [button setBackgroundImage:[UIImage imageNamed:@"btn_play_normal.png"] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"btn_play_pressed.png"] forState:UIControlStateHighlighted];
                    [button addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 1:
                    [button setBackgroundImage:[UIImage imageNamed:@"btn_delete_normal_record.png"] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"btn_delete_pressed_record.png"] forState:UIControlStateHighlighted];
                    [button addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 2:
                    [button setBackgroundImage:[UIImage imageNamed:@"btn_share_normal.png"] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"btn_share_pressed.png"] forState:UIControlStateHighlighted];
                    [button addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
                    break;
                default:
                    
                    break;
            }
        }
        //
        [bubbleView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(undo)]];
        //
        bubbleView.radius = radius;
        bubbleView.borderWidth = 2;
        bubbleView.arrowHeight = arrowHeight;
        bubbleView.arrowAngle = M_PI/6;
        bubbleView.arrowDirectorType = TLBubbleArrowType_Right;
        bubbleView.arrowLineRatio = 0.5;
        bubbleView.backgroundColor = [UIColor whiteColor];
        bubbleView.pt_Arrow = pt;
        bubbleView.borderColor = [UIColor colorWithHexString:@"4A90E2"];
        [bubbleView displayMask];
        bubbleView.alpha = 0.95;
        //
        pt = bubbleView.center;
        bubbleView.center = CGPointMake(pt.x + bubbleView.frame.size.width*0.5, pt.y);
        bubbleView.transform = CGAffineTransformMakeScale(0.000001, 1);
        [UIView animateWithDuration:0.2f animations:^{
            bubbleView.center = pt;
            bubbleView.transform = CGAffineTransformMakeScale(1, 1);
        }];
    }
}
//播放
-(void)playAction:(UIButton*)button{
    BOOL hideBubble = _clickButton(ClickButtonType_Play);
    if (hideBubble) {
        [self _tapEmpty];
    }
    [NSObject msgToClassName:@"TLMobStat" menthed:@"logEvent:eventLabel:" refs:@[RE_main,@"DIY_播放"] needReturnValue:NO];
}
//删除
-(void)deleteAction:(UIButton*)button{
    BOOL hideBubble = _clickButton(ClickButtonType_Delete);
    if (hideBubble) {
        [self _tapEmpty];
    }
    [NSObject msgToClassName:@"TLMobStat" menthed:@"logEvent:eventLabel:" refs:@[RE_main,@"DIY_删除"] needReturnValue:NO];
}
//分享
-(void)shareAction:(UIButton*)button{
    BOOL hideBubble = _clickButton(ClickButtonType_Share);
    if (hideBubble) {
        [self _tapEmpty];
    }
    [NSObject msgToClassName:@"TLMobStat" menthed:@"logEvent:eventLabel:" refs:@[RE_main,@"DIY_分享"] needReturnValue:NO];
}

-(void)undo{
    
}

@end
